package com.cbs.android.component.share.umeng;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentActivity;

import com.cbs.android.component.log.L;
import com.cbs.android.component.share.shareinterface.ShareHandler;
import com.cbs.android.component.share.shareinterface.ShareInterface;
import com.cbs.android.component.share.shareinterface.constant.SharePlatform;
import com.cbs.android.component.share.shareinterface.entity.ImageShareMessage;
import com.cbs.android.component.share.shareinterface.entity.LinkShareMessage;
import com.cbs.android.component.share.shareinterface.entity.ShareMessage;
import com.cbs.android.component.share.shareinterface.entity.TextShareMessage;
import com.cbs.android.component.share.umeng.constant.Failures;
import com.cbs.android.component.share.umeng.entity.CBSUMImage;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by broche on 5/18/16.
 */
public class UMeng implements ShareInterface {
    private static final String TAG = UMeng.class.getName();

    private ShareHandler defaultShareHandler = new ShareHandler() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onException(Exception e) {

        }

        @Override
        public void onFailure(int i, String s) {

        }
    };

    public UMeng(Context context) {
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            String qq_appkey = String.valueOf(applicationInfo.metaData.getInt("QQ_APPKEY"));
            String qq_seckey = applicationInfo.metaData.getString("QQ_SECKEY");
            String wx_appkey = applicationInfo.metaData.getString("WEIXIN_APPKEY");
            String wx_seckey = applicationInfo.metaData.getString("WEIXIN_SECKEY");
            PlatformConfig.setWeixin(wx_appkey, wx_seckey);
            PlatformConfig.setQQZone(qq_appkey, qq_seckey);
        } catch (PackageManager.NameNotFoundException e) {
            L.e(TAG, "", e);
        }
    }

    @Override
    public void share(Fragment fragment, int platforms, ShareMessage message, ShareHandler handler) {
        share(fragment.getActivity(), platforms, message, handler);
    }

    @Override
    public void share(android.support.v4.app.Fragment fragment, int platforms, ShareMessage message, ShareHandler handler) {
        share((Activity) fragment.getActivity(), platforms, message, handler);
    }

    @Override
    public void share(FragmentActivity activity, int platforms, ShareMessage message, ShareHandler handler) {
        share((Activity) activity, platforms, message, handler);
    }

    @Override
    public void share(Activity activity, int platforms, ShareMessage message, ShareHandler handler) {
        if (handler == null) {
            handler = defaultShareHandler;
        }

        List<SHARE_MEDIA> platformList = new ArrayList<>();
        if ((platforms & SharePlatform.Wechat) > 0) {
            platformList.add(SHARE_MEDIA.WEIXIN);
        }
        if ((platforms & SharePlatform.WechatMoments) > 0) {
            platformList.add(SHARE_MEDIA.WEIXIN_CIRCLE);
        }
        if ((platforms & SharePlatform.QQ) > 0) {
            platformList.add(SHARE_MEDIA.QQ);
        }
        if ((platforms & SharePlatform.QZone) > 0) {
            platformList.add(SHARE_MEDIA.QZONE);
        }

        List<SHARE_MEDIA> displaylist = new ArrayList<>();
        List<ShareContent> shareContentList = new ArrayList<>();
        List<MyShareHandler> myShareHandlerList = new ArrayList<>();
        for (int i = 0; i < platformList.size(); i++) {
            ShareContent shareContent = new ShareContent();
            if (platformList.get(i) == SHARE_MEDIA.QQ) {    // QQ
                if (TextShareMessage.class.isAssignableFrom(message.getClass())) {      // 不支持纯文本分享
                    handler.onFailure(1, Failures.getMessage(1));
                    continue;
                } else if (ImageShareMessage.class.isAssignableFrom(message.getClass())) {
                    ImageShareMessage imageShareMessage = (ImageShareMessage) message;
                    if (imageShareMessage.getImageBitmap() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageBitmap());
                        shareContent.mMedia = umImage;
                    } else if (imageShareMessage.getImageUrl() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageUrl());
                        shareContent.mMedia = umImage;
                    }
                } else if (LinkShareMessage.class.isAssignableFrom(message.getClass())) {
                    LinkShareMessage linkShareMessage = (LinkShareMessage) message;
                    shareContent.mText = linkShareMessage.getContent();
                    shareContent.mTargetUrl = linkShareMessage.getUrl();
                    if (linkShareMessage.getImageBitmap() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageBitmap());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    } else if (linkShareMessage.getImageUrl() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageUrl());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    }
                    shareContent.mTitle = linkShareMessage.getTitle() == null ? linkShareMessage.getContent() : linkShareMessage.getTitle();
                }
            } else if (platformList.get(i) == SHARE_MEDIA.QZONE) {      // QZone
                if (TextShareMessage.class.isAssignableFrom(message.getClass())) {      // 不支持纯文本分享
                    handler.onFailure(1, Failures.getMessage(1));
                    continue;
                } else if (ImageShareMessage.class.isAssignableFrom(message.getClass())) {      // 不支持纯图片分享
                    handler.onFailure(1, Failures.getMessage(1));
                    continue;
                } else if (LinkShareMessage.class.isAssignableFrom(message.getClass())) {
                    LinkShareMessage linkShareMessage = (LinkShareMessage) message;
                    shareContent.mText = linkShareMessage.getContent();
                    shareContent.mTargetUrl = linkShareMessage.getUrl();
                    if (linkShareMessage.getImageBitmap() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageBitmap());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    } else if (linkShareMessage.getImageUrl() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageUrl());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    }
                    shareContent.mTitle = linkShareMessage.getTitle() == null ? linkShareMessage.getContent() : linkShareMessage.getTitle();
                }
            } else if (platformList.get(i) == SHARE_MEDIA.WEIXIN) {     // Weixin
                if (TextShareMessage.class.isAssignableFrom(message.getClass())) {
                    TextShareMessage textShareMessage = (TextShareMessage) message;
                    shareContent.mText = textShareMessage.getContent();
                } else if (ImageShareMessage.class.isAssignableFrom(message.getClass())) {
                    ImageShareMessage imageShareMessage = (ImageShareMessage) message;
                    if (imageShareMessage.getImageBitmap() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageBitmap());
                        shareContent.mMedia = umImage;
                    } else if (imageShareMessage.getImageUrl() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageUrl());
                        shareContent.mMedia = umImage;
                    }
                } else if (LinkShareMessage.class.isAssignableFrom(message.getClass())) {
                    LinkShareMessage linkShareMessage = (LinkShareMessage) message;
                    shareContent.mText = linkShareMessage.getContent();
                    shareContent.mTargetUrl = linkShareMessage.getUrl();
                    if (linkShareMessage.getImageBitmap() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageBitmap());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    } else if (linkShareMessage.getImageUrl() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageUrl());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    }
                    shareContent.mTitle = linkShareMessage.getTitle() == null ? linkShareMessage.getContent() : linkShareMessage.getTitle();
                }
            } else if (platformList.get(i) == SHARE_MEDIA.WEIXIN_CIRCLE) {      // WeixinCircle
                if (TextShareMessage.class.isAssignableFrom(message.getClass())) {
                    TextShareMessage textShareMessage = (TextShareMessage) message;
                    shareContent.mText = textShareMessage.getContent();
                } else if (ImageShareMessage.class.isAssignableFrom(message.getClass())) {
                    ImageShareMessage imageShareMessage = (ImageShareMessage) message;
                    if (imageShareMessage.getImageBitmap() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageBitmap());
                        shareContent.mMedia = umImage;
                    } else if (imageShareMessage.getImageUrl() != null) {
                        UMImage umImage = new CBSUMImage(activity, imageShareMessage.getImageUrl());
                        shareContent.mMedia = umImage;
                    }
                } else if (LinkShareMessage.class.isAssignableFrom(message.getClass())) {
                    LinkShareMessage linkShareMessage = (LinkShareMessage) message;
                    shareContent.mTitle = linkShareMessage.getContent();
                    shareContent.mText = linkShareMessage.getContent();
                    shareContent.mTargetUrl = linkShareMessage.getUrl();
                    if (linkShareMessage.getImageBitmap() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageBitmap());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    } else if (linkShareMessage.getImageUrl() != null) {
                        CBSUMImage umImage = new CBSUMImage(activity, linkShareMessage.getImageUrl());
                        umImage.setWidth(CBSUMImage.LinkImageWidth);
                        umImage.setWidth(CBSUMImage.LinkImageQuality);
                        shareContent.mMedia = umImage;
                    }
                }
            } else {        // default
                if (TextShareMessage.class.isAssignableFrom(message.getClass())) {
                    TextShareMessage textShareMessage = (TextShareMessage) message;
                    shareContent.mText = textShareMessage.getContent();
                } else if (ImageShareMessage.class.isAssignableFrom(message.getClass())) {
                    ImageShareMessage imageShareMessage = (ImageShareMessage) message;
                    if (imageShareMessage.getImageBitmap() != null) {
                        UMImage umImage = new UMImage(activity, imageShareMessage.getImageBitmap());
                        shareContent.mMedia = umImage;
                    } else if (imageShareMessage.getImageUrl() != null) {
                        UMImage umImage = new UMImage(activity, imageShareMessage.getImageUrl());
                        shareContent.mMedia = umImage;
                    }
                } else if (LinkShareMessage.class.isAssignableFrom(message.getClass())) {
                    LinkShareMessage linkShareMessage = (LinkShareMessage) message;
                    shareContent.mText = linkShareMessage.getContent();
                    shareContent.mTargetUrl = linkShareMessage.getUrl();
                    if (linkShareMessage.getImageBitmap() != null) {
                        UMImage umImage = new UMImage(activity, linkShareMessage.getImageBitmap());
                        shareContent.mMedia = umImage;
                    } else if (linkShareMessage.getImageUrl() != null) {
                        UMImage umImage = new UMImage(activity, linkShareMessage.getImageUrl());
                        shareContent.mMedia = umImage;
                    }
                    shareContent.mTitle = linkShareMessage.getTitle() == null ? linkShareMessage.getContent() : linkShareMessage.getTitle();
                }
            }
            displaylist.add(platformList.get(i));
            shareContentList.add(shareContent);
            myShareHandlerList.add(new MyShareHandler(handler));
        }

        ShareAction shareAction = new ShareAction(activity).setDisplayList(displaylist.toArray(new SHARE_MEDIA[0]))
                .setContentList(shareContentList.toArray(new ShareContent[0])).setListenerList(myShareHandlerList.toArray(new MyShareHandler[0]));
        shareAction.open();
    }

    private static class MyShareHandler implements UMShareListener {
        private ShareHandler handler;

        public MyShareHandler(ShareHandler handler) {
            this.handler = handler;
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            handler.onSuccess();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            handler.onException(new Exception(throwable));
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            handler.onFailure(1, "已取消");
        }
    }
}
